﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.DTOs.Configuration;

public partial class Administration_ManageDepartments : BaseView, IManageDepartments
{
    ManageDepartmentsController _controller = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ManageDepartmentsController(this);
        _controller.InitView(IsPostBack);

    }

    public void grdDepartments_RowCommand(object sender, CommandEventArgs e)
    {
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        if (rowCommand == RowCommands.EditItem)
        {
            string departmentId = e.CommandArgument.ToString();
            _controller.OpenDepartment(int.Parse(departmentId));
        }
        else if (rowCommand == RowCommands.DeleteItem)
        {
            string departmentId = e.CommandArgument.ToString();
            _controller.DeleteDepartment(int.Parse(departmentId));
        }

    }

    protected void grdDepartments_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = UILiterals.DeleteDepartmentConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "DepartmentName").ToString());
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");
        }
    }


    #region IManageDepartments Members

    public List<DepartmentInformation> Departments
    {
        get
        {
            return (List<DepartmentInformation>)grdDepartments.DataSource;
        }
        set
        {
            grdDepartments.DataSource = value;
            grdDepartments.DataBind();
        }
    }

    public DepartmentInformation CurrentDepartment
    {
        get
        {
            DepartmentInformation departmentInformation = new DepartmentInformation();
            departmentInformation.DepartmentID = int.Parse(lblDepartmentIDValue.Text);
            departmentInformation.DepartmentName = txtDepartmentName.Text;
            departmentInformation.DepartmentDescription = txtDepartmentDescription.Text;
            departmentInformation.DepartmentComments = txtDepartmentComments.Text;
            departmentInformation.CompanyID = CompanyID;
            departmentInformation.CompanyName = _controller.GetCompanyForDepartment(CompanyID).CompanyName;
            return departmentInformation;
        }
        set
        {
            lblDepartmentIDValue.Text = value.DepartmentID.ToString();
            txtDepartmentName.Text = value.DepartmentName;
            txtDepartmentDescription.Text = value.DepartmentDescription;
            txtDepartmentComments.Text = value.DepartmentComments;
        }
    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible == true)
                return FormMode.List;
            else if (pnlAddEdit.Visible == true)
                return FormMode.AddEdit;
            else
                throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    #endregion
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveDepartment();

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
    }
    protected void btnAddNewDepartment_Click(object sender, EventArgs e)
    {
        _controller.OpenDepartment(0);
    }

    #region IManageDepartments Members


    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    #endregion

    #region IManageDepartments Members


    public int CompanyID
    {
        get { return int.Parse(Request.Params["id"].ToString()); }
    }

  
    #endregion
}
